package com.sgb.kjwl.view.ui.fragment.manage;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentIntellpropBrandBinding;
import com.sgb.kjwl.databinding.FragmentIntellpropForShopBrandBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.enterprise.CopyrightListEntity2;
import com.sgb.kjwl.model.entity.enterprise.IntellpropEntity;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.PatentEntity;
import com.sgb.kjwl.model.entity.manage.DataForShopCompanyTrendsEntity;
import com.sgb.kjwl.model.entity.manage.IPDetailEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.view.ui.activity.enterprise.MakeOverDetailActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.IntellpropAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.IpCopyrightAdapter;
import com.sgb.kjwl.view.ui.adapter.enterprise.PatentAdapter;
import com.sgb.kjwl.view.ui.adapter.manage.DataForCompanyShopAdapter;
import com.sgb.kjwl.view.ui.fragment.enterprise.IntellpropFragment;
import com.sgb.kjwl.view.widget.IpPopWindow;
import com.sgb.kjwl.view.widget.IpPricePopWindow;
import com.sgb.kjwl.viewmodel.manage.IntellectualPropertyRightModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class IntellectualPropertyRightFragment extends AppFragment {

    @Inject
    IntellectualPropertyRightModel model;

    private FragmentIntellpropForShopBrandBinding binding;

    private IpPopWindow brandPopWindow;//第一个筛选
    private IpPopWindow pricePopWindow1;//第二个筛选
    private IpPopWindow makeupPopWindow;//第三个筛选

    private boolean isChange1 = false,isChange2 = false,isChange3 = false;
    private boolean isLoadMore = false;
    private DataForCompanyShopAdapter ipCopyrightAdapter;
    private String no = "0";
    private int position = -1;
    private String type;
    private String typesOfPropertyRights;
    private String state;
    private int companyPageNumber;
    private String companyNo;

    public static IntellectualPropertyRightFragment newInstance(int position,String companyNo) {
        IntellectualPropertyRightFragment fragment = new IntellectualPropertyRightFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putString("companyNo", companyNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_intellprop_for_shop_brand, container, false);
        binding.setViewModel(model);

        position = getArguments().getInt("position");
        companyNo = getArguments().getString("companyNo");
        initView();
        return binding.getRoot();
    }

    private void initTab() {
//        switch (position) {
//            case 0://商标
//                typesOfPropertyRights = "商标";
//                binding.tvClass1.setVisibility(View.GONE);
//                binding.tvClass3.setVisibility(View.GONE);
//                break;
//            case 1:
//                typesOfPropertyRights = "专利";
//                binding.tvClass1.setVisibility(View.GONE);
//                binding.tvClass3.setVisibility(View.GONE);
//                break;
//            case 2:
//                typesOfPropertyRights = "版权";
//                binding.tvClass1.setVisibility(View.GONE);
//                binding.tvClass3.setVisibility(View.GONE);
//                break;
//        }
    }

    private void initView() {
        binding.tvClass1.setOnClickListener(this);
        binding.tvClass2.setOnClickListener(this);
        binding.tvClass3.setOnClickListener(this);
        if (ipCopyrightAdapter == null) {
            ipCopyrightAdapter = new DataForCompanyShopAdapter(getActivity(), null);
        }

        binding.recyclerView.setAdapter(ipCopyrightAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                setDataLoadMore(true);
            }

            @Override
            public void onLoadMore() {
                setDataLoadMore(false);
            }
        });
        initTab();
        setDataLoadMore(true);


//        brandPopWindow = new IpPopWindow(getActivity());
//        brandPopWindow.setOnResultClick((type, key2, key3) -> {
////            binding.tvClass1.setText(((IpPickSelectEntity) type).getName());
////            setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.pick_select_down, R.color.new_blue);
//            String name = ((IpPickSelectEntity)type).getName();
//            if (!name.equals("不限")){
//                isChange1 = true;
//                setTextViewBg(binding.tvClass1, R.drawable.search_new_blue_bg, R.mipmap.pick_select_down, R.color.new_blue);
//            }else {
//                isChange1 = false;
//                setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
//            }
//            setDataLoadMore(true);
//        });
//        brandPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                if (!isChange1){
//                    setTextViewBg(binding.tvClass1, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
//                }
//            }
//        });

        pricePopWindow1 = new IpPopWindow(getActivity());
        pricePopWindow1.setOnResultClick((type, key2, key3) -> {
//            binding.tvClass2.setText(((IpPickSelectEntity) type).getName());
//            setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.pick_select_down, R.color.new_blue);
            String name = ((IpPickSelectEntity)type).getName();
            if (!name.equals("不限")){
                isChange2 = true;
                IntellectualPropertyRightFragment.this.type = ((IpPickSelectEntity) type).getName();
//                setTextViewBg(binding.tvClass2, R.drawable.search_new_blue_bg, R.mipmap.pick_select_down, R.color.new_blue);
            }else {
                isChange2 = false;
                IntellectualPropertyRightFragment.this.type = "";
//                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
            }
            setDataLoadMore(true);
        });
        pricePopWindow1.setOnDismissListener(() -> {
            if (!isChange2){
//                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
            }
        });

        makeupPopWindow = new IpPopWindow(getActivity());
        makeupPopWindow.setOnResultClick((type, key2, key3) -> {
//            binding.tvClass3.setText(((IpPickSelectEntity) type).getName());
//            setTextViewBg(binding.tvClass3, R.drawable.search_new_blue_bg, R.mipmap.pick_select_down, R.color.new_blue);
            String name = ((IpPickSelectEntity)type).getName();
            if (!name.equals("不限")){
                isChange3 = true;
//                setTextViewBg(binding.tvClass3, R.drawable.search_new_blue_bg, R.mipmap.pick_select_down, R.color.new_blue);
            }else {
                isChange3 = false;
//                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
            }
            state = ((IpPickSelectEntity) type).getValue();
            setDataLoadMore(true);
        });
        makeupPopWindow.setOnDismissListener(() -> {
            if (!isChange3){
//                setTextViewBg(binding.tvClass3, R.drawable.search_bg, R.mipmap.ic_down_arrow_n, R.color.color_333333);
            }
        });

//        brandPopWindow.setBgView(binding.grayLayout);
//        pricePopWindow1.setBgView(binding.grayLayout);
//        makeupPopWindow.setBgView(binding.grayLayout);

    }

    private void setDataLoadMore(boolean isRefresh) {
        if (isRefresh) {
            isLoadMore = false;
            companyPageNumber = 1;
        } else {
            isLoadMore = true;
            companyPageNumber++;
        }
        Map<String, Object> map4 = new HashMap<>();
        map4.put("companyNo", companyNo);
        map4.put("state", state);
        map4.put("typesOfPropertyRights", typesOfPropertyRights);
        map4.put("type", type);
        map4.put("page", companyPageNumber);
        map4.put("limit", 20);
        model.getCopyrightList(map4);
    }

    /**
     * 版本数据
     *
     * @param list
     */
    public void showCopyrightList(List<IPDetailEntity> list) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                ipCopyrightAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                ipCopyrightAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                ipCopyrightAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_class1:
                model.queryIndustryCategry();
                break;
            case R.id.tv_class2:
//                setTextViewBg(binding.tvClass2, R.drawable.search_bg, R.mipmap.pick_select_down, R.color.new_blue);
                switch (position) {
                    case 0:
                        model.getTrademarkTypeOption();
                        break;
                    case 1:
                        model.getPatentTypeOption();
                        break;
                    case 2:
                        model.getCopyrightTypeOption();
                        break;
                }
                break;
            case R.id.tv_class3:
                switch (position) {
                    case 0:
                    case 2:
                    case 1:
                        model.getIsOnlyOption();
                        break;
                }
                break;
            default:
                break;
        }
    }


    public void showBrandPopWindow(List<IpPickSelectEntity> brandDatas) {
        brandPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        brandPopWindow.setData(brandDatas);
    }

    public void showPricePopWindow1(List<IpPickSelectEntity> priceDatas1) {
        pricePopWindow1.showPopwindow(binding.roomLayout, 0, 0);
        pricePopWindow1.setData(priceDatas1);
    }

    public void showMakeupPopWindow(List<IpPickSelectEntity> makeupDatas) {
        makeupPopWindow.showPopwindow(binding.roomLayout, 0, 0);
        makeupPopWindow.setData(makeupDatas);
    }

    private void setTextViewBg(TextView textView, int bg, int img, int textColor) {
        textView.setBackgroundResource(bg);
        Drawable drawable_n = getResources().getDrawable(img);
        drawable_n.setBounds(0, 0, drawable_n.getMinimumWidth(), drawable_n.getMinimumHeight());
        textView.setTextColor(getResources().getColor(textColor));
    }
}
